/*aN15sYJ6k0P3KiflGhn5gT/aBgdCGmx6bjrTvZYkcg2CEdT2PPPE/LQxWw/9vtEw*/
/*
 * @Author: llixxm
 * @Date: 2020-05-08 17:02:18
 * @Description: 账表冻结事件
 */
import { toast } from 'nc-lightapp-front';
/**
 * @description: 冻结/取消冻结 点击事件
 * @param {btnName} 按钮Id
 * @return: 
 */ 
function freezeClick(btnName){
    if(btnName === 'autoWidth'){ //自动调整列宽
        this.refs.balanceTable.autoWidths();
    } else {
        updateFreezeData(this, btnName); //冻结相关事件
    }
    // if(btnName === 'lockCancel'){
    //     this.refs.balanceTable.freezeFun('cancel'); //取消冻结
    //     updateFreezeStatus(this, false)
    // } else {
    //     updateFreezeData(this, btnName);
    // }
}
/**
 * @description: 冻结事件处理,更新冻结数据
 * @param {btnName} 按钮Id
 * @return: 
 */
const updateFreezeData = (page, btnName) => {
    let { dataout } = page.state;
    let info = page.refs.balanceTable.getReportInfo();
    if(btnName !== 'lockCancel' && typeof info.coords === 'undefined'){
        return false
    }
    let isFreeze = true;
    let isOverScreen = isoverScreen(page, dataout, info, btnName);
    if(!isOverScreen){
        switch(btnName){
            case 'lockCancel': //取消冻结
                page.refs.balanceTable.freezeFun('cancel'); //取消冻结
                dataout.data["freezing"].isFreeze = false;
                dataout.data["freezing"].row = 0;
                dataout.data["freezing"].col = 0;
                isFreeze = false;
            break;
            case 'lockRow': //冻结行
                dataout.data["freezing"].isFreeze = true;
                dataout.data["freezing"].row = info.coords.row;
                dataout.data["freezing"].col = 0;
                isFreeze = true;
            break;
            case 'lockColumn': //冻结列
                dataout.data["freezing"].isFreeze = true;
                dataout.data["freezing"].col = info.coords.col;
                dataout.data["freezing"].row = 0;
                isFreeze = true;
            break;
            case 'lockRowColumn': //冻结行列
                dataout.data["freezing"].isFreeze = true;
                dataout.data["freezing"].row = info.coords.row;
                dataout.data["freezing"].col = info.coords.col;
                isFreeze = true;
            break;
        }
        page.setState({ dataout }, () => {
            updateFreezeStatus(page, isFreeze);
        });
    }
}

/**
 * @description: 更新冻结按钮状态
 * @param {isFreeze} 是否是冻结操作, true:冻结, false:取消冻结
 * @return: 
 */
const updateFreezeStatus = (page, isFreeze) => {
    page.refs.balanceTable.updateSettings() //更新表格,重新渲染(平台方法)
    page.props.button.setButtonDisabled(['lock'], isFreeze);
    page.props.button.setButtonDisabled(['lockCancel'], !isFreeze);
}

const isoverScreen = (page, dataout, info, btnName) => {
    let isoverScreen = false;
    let tableHeigth = document.getElementById('simpleTableReport').offsetHeight;
    let tableWidth = document.getElementById('simpleTableReport').offsetWidth;
    let widthArr = dataout.data.widths && dataout.data.widths.map(item => Number(item.colwidth)) || [];
    let rowNum = info.coords && info.coords.row > 0 ?  info.coords.row + 2 : 1;
    let colNum = info.coords && info.coords.col > 0 ? info.coords.col + 1 : 1;
    let overflowHeight = arraySum(dataout.data.rowHeights.slice(0, rowNum));
    let overflowWidth = arraySum(widthArr.slice(0, colNum));
    if(overflowHeight > tableHeigth && (btnName === 'lockRow' || btnName === 'lockRowColumn')){
        toast({content: page.state.json['publiccommon-000017'], color: 'warning'})
        isoverScreen = true;
    } //当前位置超出一屏,暂不支持冻结
    
    if(overflowWidth > tableWidth && (btnName === 'lockColumn' || btnName === 'lockRowColumn')){
        toast({content: page.state.json['publiccommon-000017'], color: 'warning'})
        isoverScreen = true;
    } //当前位置超出一屏,暂不支持冻结
    return isoverScreen;
}

const arraySum = (arr) =>{
    if(arr && arr.length > 0){
        return arr.reduce(function (prev, curr, idx, arr) {
            return prev + curr;
        });
    } else {
        return 100;
    }
    
}

export { freezeClick }

/*aN15sYJ6k0P3KiflGhn5gT/aBgdCGmx6bjrTvZYkcg2CEdT2PPPE/LQxWw/9vtEw*/